#include "stdafx.h"
#include <gdiplus.h>
#include "gdiplustest2.h"

#include "gdiplustest2Doc.h"
#include "gdiplustest2View.h"
#include ".\gdiplustest2view.h"
#include "MainFrm.h"
#include "MyUtil.h"
#include <assert.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNCREATE(Cgdiplustest2View, CView)

BEGIN_MESSAGE_MAP(Cgdiplustest2View, CView)
    ON_COMMAND(ID_MAG_1, OnMag1)
    ON_COMMAND(ID_MAG_2, OnMag2)
    ON_COMMAND(ID_MAG_3, OnMag3)
    ON_COMMAND(ID_MAG_4, OnMag4)
    ON_COMMAND(ID_IM_0, OnIm0)
    ON_COMMAND(ID_IM_1, OnIm1)
    ON_COMMAND(ID_IM_2, OnIm2)
    ON_COMMAND(ID_IM_3, OnIm3)
    ON_COMMAND(ID_IM_4, OnIm4)
    ON_COMMAND(ID_IM_5, OnIm5)
    ON_COMMAND(ID_IM_6, OnIm6)
    ON_COMMAND(ID_IM_7, OnIm7)
    ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

Cgdiplustest2View::Cgdiplustest2View()
{
    interpolationMode = Gdiplus::InterpolationModeHighQualityBicubic;
    zoom = 1;
}

Cgdiplustest2View::~Cgdiplustest2View()
{
}

void Cgdiplustest2View::MenuItemUpdate(void)
{
    CWnd* pMain = AfxGetMainWnd();
    assert(pMain);
    CMenu* pMenu = pMain->GetMenu();
    assert(pMenu);
    pMenu->CheckMenuItem(ID_MAG_1, (zoom==1)?MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
    pMenu->CheckMenuItem(ID_MAG_2, (zoom==2)?MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
    pMenu->CheckMenuItem(ID_MAG_3, (zoom==3)?MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
    pMenu->CheckMenuItem(ID_MAG_4, (zoom==4)?MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
    pMenu->CheckMenuItem(ID_IM_0, (interpolationMode == Gdiplus::InterpolationModeDefault)?            MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
    pMenu->CheckMenuItem(ID_IM_1, (interpolationMode == Gdiplus::InterpolationModeLowQuality)?         MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
    pMenu->CheckMenuItem(ID_IM_2, (interpolationMode == Gdiplus::InterpolationModeHighQuality)?        MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
    pMenu->CheckMenuItem(ID_IM_3, (interpolationMode == Gdiplus::InterpolationModeBilinear)?           MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
    pMenu->CheckMenuItem(ID_IM_4, (interpolationMode == Gdiplus::InterpolationModeBicubic)?            MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
    pMenu->CheckMenuItem(ID_IM_5, (interpolationMode == Gdiplus::InterpolationModeNearestNeighbor)?    MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
    pMenu->CheckMenuItem(ID_IM_6, (interpolationMode == Gdiplus::InterpolationModeHighQualityBilinear)?MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
    pMenu->CheckMenuItem(ID_IM_7, (interpolationMode == Gdiplus::InterpolationModeHighQualityBicubic)? MF_BYCOMMAND | MF_CHECKED:MF_BYCOMMAND | MF_UNCHECKED);
}

BOOL Cgdiplustest2View::PreCreateWindow(CREATESTRUCT& cs)
{
    return CView::PreCreateWindow(cs);
}

SIZE Cgdiplustest2View::CalcDispSize(int imageW, int imageH, int windowW, int windowH)
{
    SIZE size;
    size.cx = windowW;
    size.cy = windowH;
    if ((float)windowW / windowH < (float)imageW / imageH) {
        size.cy = imageH * windowW / imageW;
    } else {
        size.cx = imageW * windowH / imageH;
    }
    size.cx *= zoom;
    size.cy *= zoom;
    return size;
}

void Cgdiplustest2View::OnDraw(CDC* pDC)
{
    RECT clientRect;
    GetClientRect(&clientRect);
    SIZE clientSize;
    clientSize.cx = clientRect.right - clientRect.left;
    clientSize.cy = clientRect.bottom - clientRect.top;

    Cgdiplustest2Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    Gdiplus::Image *pImage = pDoc->ImageGet();
    if (!pImage || clientSize.cx == 0 || clientSize.cy == 0) {
        return; 
    }
    Gdiplus::Graphics myGraphics(pDC->GetSafeHdc());
    myGraphics.SetInterpolationMode(interpolationMode);
    UINT imageW = pImage->GetWidth();
    UINT imageH = pImage->GetHeight();
    SIZE dispSize = CalcDispSize(imageW, imageH, clientSize.cx, clientSize.cy);

    myGraphics.DrawImage(pImage,
                        Gdiplus::Rect(0, 0, dispSize.cx, dispSize.cy),
                        0, 0,
                        imageW, imageH, Gdiplus::UnitPixel);
}

BOOL Cgdiplustest2View::OnEraseBkgnd(CDC *pDC)
{
    Cgdiplustest2Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    Gdiplus::Image *pImage = pDoc->ImageGet();
    if (!pImage) {
        return CView::OnEraseBkgnd(pDC); // AppWizard generated code
    }
    return FALSE; // Do not erase the background
}

#ifdef _DEBUG
void Cgdiplustest2View::AssertValid() const
{
    CView::AssertValid();
}

void Cgdiplustest2View::Dump(CDumpContext& dc) const
{
    CView::Dump(dc);
}

Cgdiplustest2Doc* Cgdiplustest2View::GetDocument() const
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(Cgdiplustest2Doc)));
    return (Cgdiplustest2Doc*)m_pDocument;
}
#endif //_DEBUG

void Cgdiplustest2View::OnMag1() {
    zoom = 1;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
void Cgdiplustest2View::OnMag2() {
    zoom = 2;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
void Cgdiplustest2View::OnMag3() {
    zoom = 3;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
void Cgdiplustest2View::OnMag4() {
    zoom = 4;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
void Cgdiplustest2View::OnIm0() {
    interpolationMode = Gdiplus::InterpolationModeDefault;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
void Cgdiplustest2View::OnIm1() {
    interpolationMode = Gdiplus::InterpolationModeLowQuality;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
void Cgdiplustest2View::OnIm2() {
    interpolationMode = Gdiplus::InterpolationModeHighQuality;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
void Cgdiplustest2View::OnIm3() {
    interpolationMode = Gdiplus::InterpolationModeBilinear;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
void Cgdiplustest2View::OnIm4() {
    interpolationMode = Gdiplus::InterpolationModeBicubic;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
void Cgdiplustest2View::OnIm5() {
    interpolationMode = Gdiplus::InterpolationModeNearestNeighbor;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
void Cgdiplustest2View::OnIm6() {
    interpolationMode = Gdiplus::InterpolationModeHighQualityBilinear;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
void Cgdiplustest2View::OnIm7() {
    interpolationMode = Gdiplus::InterpolationModeHighQualityBicubic;
    InvalidateRect(NULL, FALSE);
    MenuItemUpdate();
}
